#!/usr/bin/env python

import re
import sys
import numpy as np
from numpy.linalg import norm

def distance(v1, v2):
    return np.dot(v1, v2) / (norm(v1) * norm(v2))

def main(*args):
    if (len(args)) < 2:
        print("Give me two files as arguments")
        return 0

    numre = re.compile(r'\b[0-9]+')
    texts = tuple(open(arg).read() for arg in args)
    matches = tuple([int(str(match)) for match in numre.findall(text)] for text in texts)

    allmatches = set()
    for lst in matches:
        allmatches.update(lst)
    allmatches = list(allmatches)
    allmatches.sort()

    allmatchvector = np.array(allmatches)
    zerovector = np.zeros(len(allmatches), int)
    matchvectors = tuple([np.array(zerovector) for i in range(len(matches))])

    for i, matchset in enumerate(matches):
        for match in matchset:
            matchvectors[i][np.searchsorted(allmatchvector, match)] += 1
    return distance(*matchvectors)
    
if __name__ == "__main__":
    exit(not main(sys.argv[1:]))
